package com.dycx.user.service.impl;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dycx.user.converter.ReportConverter;
import com.dycx.user.mapper.ReportMapper;
import com.dycx.user.dto.ReportDTO;
import com.dycx.user.entity.ReportDO;
import com.dycx.user.service.ReportService;
import com.dycx.user.vo.ReportVO;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * 收藏(Report)表服务实现类
 *
 * @author wuzongbing
 * @since 2024-04-17 15:51:44
 */

@Service
@AllArgsConstructor
public class ReportImpl extends ServiceImpl<ReportMapper, ReportDO> implements ReportService {

    private ReportMapper reportMapper;

    /**
     * 分页查询
     *
     * @param dto 实例对象
     * @return 查询结果
     */
    @Override
    public Page<ReportVO> getPage(ReportDTO dto) {
        LambdaQueryWrapper<ReportDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ObjectUtil.isNotNull(dto.getId()), ReportDO::getId, dto.getId());
        wrapper.orderByDesc(ReportDO::getId);

        Page<ReportDO> entityPage = page(new Page<>(dto.getPageIndex(), dto.getPageSize()), wrapper);

        Page<ReportVO> voPage = ReportConverter.entityToVoPage(entityPage);
        voPage.getRecords().forEach(this::setData);

        return voPage;
    }

    /**
     * 查询详情
     *
     * @param id 主键
     * @return 查询结果
     */
    @Override
    public ReportVO load(Integer id) {
        ReportDO entity = getById(id);

        return setData(ReportConverter.entityToVo(entity));
    }

    /**
     * 设置数据
     *
     * @param vo 实体
     * @return 查询结果
     */
    public ReportVO setData(ReportVO vo) {
        return vo;
    }

    /**
     * 添加
     *
     * @param dto
     * @return
     */
    @Override
    public Boolean create(ReportDTO dto) {
        ReportDO entity = BeanUtil.toBean(dto, ReportDO.class);
        return save(entity);
    }

    /**
     * 修改
     *
     * @param dto
     * @return
     */
    @Override
    public Boolean edit(ReportDTO dto) {
        ReportDO entity = BeanUtil.toBean(dto, ReportDO.class);
        return updateById(entity);
    }

    /**
     * 删除
     *
     * @param id 主键
     * @return
     */
    @Override
    public Boolean delete(Integer id) {
        return removeById(id);
    }

    /**
     * 获取最大排序
     *
     * @return 查询结果
     */
    @Override
    public Integer getMaxSort() {
        return reportMapper.getMaxSort();
    }
}

